<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next-haha.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next-haha.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next-haha.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ming.theyan.gs","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.25.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="缘起大家好，我是老杨。在这篇文章中，我将带大家深入了解如何利用 GitHub Action 自动化部署 Chalice 应用到 AWS Lambda。这不仅是一个技术实践，也是对 CI&#x2F;CD 流程优化的一次探索。 在现代软件开发中，快速迭代和持续部署是提高开发效率的关键。Chalice 是一个用于部署 Python 应用到 AWS Lambda 的框架，而 GitHub Action 提">
<meta property="og:type" content="article">
<meta property="og:title" content="自动化部署的艺术：用 GitHub Action 部署 Python 编写的 Chalice 应用到 AWS Lambda">
<meta property="og:url" content="https://ming.theyan.gs/2023/08/%E7%94%A8%20GitHub%20Action%20%E9%83%A8%E7%BD%B2%20Python%20%E4%BB%A3%E7%A0%81%E7%BC%96%E5%86%99%E7%9A%84%20Chalice%20%E5%BA%94%E7%94%A8%E5%88%B0%20AWS%20Lambda/index.html">
<meta property="og:site_name" content="运维烂笔头">
<meta property="og:description" content="缘起大家好，我是老杨。在这篇文章中，我将带大家深入了解如何利用 GitHub Action 自动化部署 Chalice 应用到 AWS Lambda。这不仅是一个技术实践，也是对 CI&#x2F;CD 流程优化的一次探索。 在现代软件开发中，快速迭代和持续部署是提高开发效率的关键。Chalice 是一个用于部署 Python 应用到 AWS Lambda 的框架，而 GitHub Action 提">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-08-12T11:43:38.000Z">
<meta property="article:modified_time" content="2023-08-17T13:39:36.000Z">
<meta property="article:author" content="老杨">
<meta property="article:tag" content="Chalice">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="DevOps">
<meta property="article:tag" content="CI&#x2F;CD">
<meta property="article:tag" content="GitHub-Action">
<meta property="article:tag" content="AWS-Lambda">
<meta property="article:tag" content="Automation">
<meta property="article:tag" content="Deployment">
<meta property="article:tag" content="Serverless">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://ming.theyan.gs/2023/08/%E7%94%A8%20GitHub%20Action%20%E9%83%A8%E7%BD%B2%20Python%20%E4%BB%A3%E7%A0%81%E7%BC%96%E5%86%99%E7%9A%84%20Chalice%20%E5%BA%94%E7%94%A8%E5%88%B0%20AWS%20Lambda/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://ming.theyan.gs/2023/08/%E7%94%A8%20GitHub%20Action%20%E9%83%A8%E7%BD%B2%20Python%20%E4%BB%A3%E7%A0%81%E7%BC%96%E5%86%99%E7%9A%84%20Chalice%20%E5%BA%94%E7%94%A8%E5%88%B0%20AWS%20Lambda/index.html","path":"2023/08/用 GitHub Action 部署 Python 代码编写的 Chalice 应用到 AWS Lambda/index.html","title":"自动化部署的艺术：用 GitHub Action 部署 Python 编写的 Chalice 应用到 AWS Lambda"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>自动化部署的艺术：用 GitHub Action 部署 Python 编写的 Chalice 应用到 AWS Lambda | 运维烂笔头</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-106574959-2"></script>
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-106574959-2","only_pageview":false,"measure_protocol_api_secret":null}</script>
  <script src="/js/third-party/analytics/google-analytics.js" defer></script>

  <script src="/js/third-party/analytics/baidu-analytics.js" defer></script>
  <script async src="https://hm.baidu.com/hm.js?fdef9ded31bdb8b2dab08eddebdd5fed"></script>







  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script><script src="/js/bookmark.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  




<!-- google adsense -->
<script data-ad-client="ca-pub-1045025618858716" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="运维烂笔头" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">运维烂笔头</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">一个 SA 老兵的工作日志</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-projects"><a href="/projects" rel="section"><i class="fa fa-code fa-fw"></i>projects</a></li><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%98%E8%B5%B7"><span class="nav-number">1.</span> <span class="nav-text">缘起</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Chalice-%E7%AE%80%E4%BB%8B"><span class="nav-number">2.</span> <span class="nav-text">Chalice 简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GitHub-Action-%E5%B7%A5%E4%BD%9C%E6%B5%81%E8%AF%A6%E8%A7%A3"><span class="nav-number">3.</span> <span class="nav-text">GitHub Action 工作流详解</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GitHub-Action-%E7%BC%93%E5%AD%98%E5%8A%9F%E8%83%BD"><span class="nav-number">4.</span> <span class="nav-text">GitHub Action 缓存功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CI-CD-%E4%BB%93%E5%BA%93%E4%B8%AD%E7%9A%84-JSON-%E6%96%87%E4%BB%B6%E6%8E%A8%E9%80%81"><span class="nav-number">5.</span> <span class="nav-text">CI&#x2F;CD 仓库中的 JSON 文件推送</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E8%AF%AD"><span class="nav-number">6.</span> <span class="nav-text">结语</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">老杨</p>
  <div class="site-description" itemprop="description">好记性比不过烂笔头</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">114</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">509</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/haw-haw" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;haw-haw" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:blog@theyan.gs" title="E-Mail → mailto:blog@theyan.gs" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/1494877243" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;1494877243" rel="noopener me" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://twitter.com/6fool" title="Twitter → https:&#x2F;&#x2F;twitter.com&#x2F;6fool" rel="noopener me" target="_blank"><i class="fab fa-twitter fa-fw"></i>Twitter</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://bad-pencil.github.io/" title="https:&#x2F;&#x2F;bad-pencil.github.io" rel="noopener" target="_blank">github 镜像站</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://hawhaw.gitee.io/" title="https:&#x2F;&#x2F;hawhaw.gitee.io" rel="noopener" target="_blank">gitee 镜像站</a>
            </li>
        </ul>
      </div>
    </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ming.theyan.gs/2023/08/%E7%94%A8%20GitHub%20Action%20%E9%83%A8%E7%BD%B2%20Python%20%E4%BB%A3%E7%A0%81%E7%BC%96%E5%86%99%E7%9A%84%20Chalice%20%E5%BA%94%E7%94%A8%E5%88%B0%20AWS%20Lambda/index.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="老杨">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="运维烂笔头">
      <meta itemprop="description" content="好记性比不过烂笔头">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="自动化部署的艺术：用 GitHub Action 部署 Python 编写的 Chalice 应用到 AWS Lambda | 运维烂笔头">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          自动化部署的艺术：用 GitHub Action 部署 Python 编写的 Chalice 应用到 AWS Lambda
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-08-12 19:43:38" itemprop="dateCreated datePublished" datetime="2023-08-12T19:43:38+08:00">2023-08-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-08-17 21:39:36" itemprop="dateModified" datetime="2023-08-17T21:39:36+08:00">2023-08-17</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="缘起"><a href="#缘起" class="headerlink" title="缘起"></a>缘起</h2><p>大家好，我是老杨。在这篇文章中，我将带大家深入了解如何利用 GitHub Action 自动化部署 Chalice 应用到 AWS Lambda。这不仅是一个技术实践，也是对 CI&#x2F;CD 流程优化的一次探索。</p>
<p>在现代软件开发中，快速迭代和持续部署是提高开发效率的关键。Chalice 是一个用于部署 Python 应用到 AWS Lambda 的框架，而 GitHub Action 提供了一个强大的自动化平台。结合这两者，我们可以创建一个无缝的部署流程。</p>
<span id="more"></span>

<h2 id="Chalice-简介"><a href="#Chalice-简介" class="headerlink" title="Chalice 简介"></a>Chalice 简介</h2><p>Chalice 是一个 Python 框架，它使得在 AWS Lambda 和 API Gateway 上部署无服务器应用变得简单。它允许开发者使用熟悉的 Python 语法来定义 Lambda 函数和 RESTful API，同时提供了丰富的配置选项来满足不同的部署需求。</p>
<h2 id="GitHub-Action-工作流详解"><a href="#GitHub-Action-工作流详解" class="headerlink" title="GitHub Action 工作流详解"></a>GitHub Action 工作流详解</h2><p>让我们来看一下这个工作流的核心内容。以下是一个简化的工作流文件示例，它展示了如何配置和执行部署任务。</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">name:</span> <span class="string">Deploy</span> <span class="string">xxxxx-indexer</span> <span class="string">manually</span> <span class="string">using</span> <span class="string">Chalice</span></span><br><span class="line"><span class="attr">run-name:</span> <span class="string">Deploy</span> <span class="string">xxxxx-indexer</span> <span class="string">$&#123;&#123;</span> <span class="string">inputs.branch</span> <span class="string">&#125;&#125;-&gt;$&#123;&#123;</span> <span class="string">inputs.environment</span> <span class="string">&#125;&#125;</span> <span class="string">by</span> <span class="string">@$&#123;&#123;</span> <span class="string">github.actor</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">on:</span></span><br><span class="line">  <span class="attr">workflow_dispatch:</span></span><br><span class="line">    <span class="attr">inputs:</span></span><br><span class="line">      <span class="attr">branch:</span></span><br><span class="line">        <span class="attr">description:</span> <span class="string">&#x27;Branch to deploy&#x27;</span></span><br><span class="line">        <span class="attr">required:</span> <span class="literal">true</span></span><br><span class="line">        <span class="attr">default:</span> <span class="string">&#x27;v1.1&#x27;</span></span><br><span class="line">      <span class="attr">environment:</span></span><br><span class="line">        <span class="attr">description:</span> <span class="string">&#x27;Deployment environment&#x27;</span></span><br><span class="line">        <span class="attr">required:</span> <span class="literal">true</span></span><br><span class="line">        <span class="attr">default:</span> <span class="string">&#x27;dev&#x27;</span></span><br><span class="line">        <span class="attr">type:</span> <span class="string">choice</span></span><br><span class="line">        <span class="attr">options:</span></span><br><span class="line">          <span class="bullet">-</span> <span class="string">prod</span></span><br><span class="line">          <span class="bullet">-</span> <span class="string">dev</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line">  <span class="attr">env_vars:</span> <span class="string">&#x27;&#123;&quot;prod&quot;: &quot;1111&quot;, &quot;dev&quot;: &quot;2222&quot;&#125;&#x27;</span></span><br><span class="line">  <span class="attr">project_directory:</span> <span class="string">&#x27;xxxx-indexer&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line">  <span class="attr">deploy-job:</span></span><br><span class="line">    <span class="attr">runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"></span><br><span class="line">    <span class="attr">steps:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Checkout</span> <span class="string">code</span></span><br><span class="line">        <span class="attr">uses:</span> <span class="string">actions/checkout@v4</span></span><br><span class="line">        <span class="attr">with:</span></span><br><span class="line">          <span class="attr">token:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.TOKEN_CICD</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">ref:</span> <span class="string">$&#123;&#123;</span> <span class="string">github.event.inputs.branch</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">sparse-checkout:</span> <span class="string">$&#123;&#123;</span> <span class="string">env.project_directory</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">sparse-checkout-cone-mode:</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Delete</span> <span class="string">configuration</span> <span class="string">files</span> <span class="string">for</span> <span class="string">chalice</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          [[ -e $&#123;&#123; env.project_directory &#125;&#125;/.chalice ]] &amp;&amp; \</span></span><br><span class="line"><span class="string">            rm -rf $&#123;&#123; env.project_directory &#125;&#125;/.chalice</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Checkout</span> <span class="string">configuration</span> <span class="string">files</span> <span class="string">for</span> <span class="string">CI/CD</span></span><br><span class="line">        <span class="attr">uses:</span> <span class="string">actions/checkout@v4</span></span><br><span class="line">        <span class="attr">with:</span></span><br><span class="line">          <span class="attr">repository:</span> <span class="string">xxxx-xxxx/CICD</span></span><br><span class="line">          <span class="attr">token:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.TOKEN_CICD</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">ref:</span> <span class="string">main</span></span><br><span class="line">          <span class="attr">sparse-checkout:</span> <span class="string">chalice/$&#123;&#123;</span> <span class="string">env.project_directory</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">sparse-checkout-cone-mode:</span> <span class="literal">false</span></span><br><span class="line">          <span class="attr">path:</span> <span class="string">config-chalice</span></span><br><span class="line"></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Make</span> <span class="string">a</span> <span class="string">symbolic(soft)</span> <span class="string">link</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          ln -srv \</span></span><br><span class="line"><span class="string">             config-chalice/chalice/$&#123;&#123; env.project_directory &#125;&#125;/.chalice \</span></span><br><span class="line"><span class="string">             $&#123;&#123; env.project_directory &#125;&#125;/</span></span><br><span class="line"><span class="string">          echo &quot;after checkout CI/CD:&quot;</span></span><br><span class="line"><span class="string">          ls -lRa $&#123;&#123; env.project_directory &#125;&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Set</span> <span class="string">up</span> <span class="string">Python</span></span><br><span class="line">        <span class="attr">uses:</span> <span class="string">actions/setup-python@v5</span></span><br><span class="line">        <span class="attr">with:</span></span><br><span class="line">          <span class="attr">python-version:</span> <span class="string">&#x27;3.9&#x27;</span></span><br><span class="line"></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">uses:</span> <span class="string">actions/cache@v4</span></span><br><span class="line">        <span class="attr">with:</span></span><br><span class="line">          <span class="attr">path:</span> <span class="string">~/.cache/pip</span></span><br><span class="line">          <span class="attr">key:</span> <span class="string">$&#123;&#123;</span> <span class="string">runner.os</span> <span class="string">&#125;&#125;-pip-$&#123;&#123;</span> <span class="string">hashFiles(&#x27;**/requirements.txt&#x27;)</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">restore-keys:</span> <span class="string">|</span></span><br><span class="line"><span class="string">            $&#123;&#123; runner.os &#125;&#125;-pip-</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Install</span> <span class="string">dependencies</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          cd $&#123;&#123; env.project_directory &#125;&#125;</span></span><br><span class="line"><span class="string">          pip install -r requirements.txt</span></span><br><span class="line"><span class="string">          pip install chalice</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Prepare</span> <span class="string">the</span> <span class="string">.chalice/config.json</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          cd $&#123;&#123; env.project_directory &#125;&#125;</span></span><br><span class="line"><span class="string">          sed -e &quot;s:___GH-REF___:$&#123;&#123; github.event.inputs.branch &#125;&#125;:g&quot; \</span></span><br><span class="line"><span class="string">              -e &quot;s/___GH-COMMIT-ID___/$(git log -1 --format=&#x27;%H&#x27;)/g&quot; \</span></span><br><span class="line"><span class="string">              -e &quot;s/___DBPASSWORD___/$&#123;&#123; secrets[format(&#x27;DB_PWD_&#123;0&#125;&#x27;, fromJson(env.env_vars)[github.event.inputs.environment])] &#125;&#125;/g&quot; \</span></span><br><span class="line"><span class="string">              -e &quot;s:___GH-REPOSITORY___:$&#123;&#123; github.repository &#125;&#125;:g&quot; \</span></span><br><span class="line"><span class="string">              -e &quot;s:___GH-DIRECTORY___:$&#123;&#123; env.project_directory &#125;&#125;:g&quot; \</span></span><br><span class="line"><span class="string">              -i .chalice/config.json</span></span><br><span class="line"><span class="string">          echo &quot;the content of file .chalice/config.json:&quot;</span></span><br><span class="line"><span class="string">          cat .chalice/config.json</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Configure</span> <span class="string">AWS</span> <span class="string">Credentials</span></span><br><span class="line">        <span class="attr">uses:</span> <span class="string">aws-actions/configure-aws-credentials@v4</span></span><br><span class="line">        <span class="attr">with:</span></span><br><span class="line">          <span class="attr">aws-region:</span> <span class="string">$&#123;&#123;</span> <span class="string">vars[format(&#x27;AWS_REGION_&#123;0&#125;&#x27;,</span> <span class="string">fromJson(env.env_vars)[github.event.inputs.environment])]</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">aws-access-key-id:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets[format(&#x27;AWS_ACCESS_KEY_ID_&#123;0&#125;&#x27;,</span> <span class="string">fromJson(env.env_vars)[github.event.inputs.environment])]</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="attr">aws-secret-access-key:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets[format(&#x27;AWS_SECRET_ACCESS_KEY_&#123;0&#125;&#x27;,</span> <span class="string">fromJson(env.env_vars)[github.event.inputs.environment])]</span> <span class="string">&#125;&#125;</span></span><br><span class="line"></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Deploy</span> <span class="string">Chalice</span></span><br><span class="line">        <span class="attr">id:</span> <span class="string">deploy-step</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          cd $&#123;&#123; env.project_directory &#125;&#125;</span></span><br><span class="line"><span class="string">          chalice deploy --stage $&#123;&#123; github.event.inputs.environment &#125;&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Check</span> <span class="string">for</span> <span class="string">files</span> <span class="string">changed</span></span><br><span class="line">        <span class="attr">id:</span> <span class="string">git_status</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          cd config-chalice/chalice/$&#123;&#123; env.project_directory &#125;&#125;/.chalice</span></span><br><span class="line"><span class="string">          git status -s</span></span><br><span class="line"><span class="string">          file_changed=$(if git status -s | grep -q &quot;deployed/$&#123;&#123; github.event.inputs.environment &#125;&#125;.json&quot;; then echo &#x27;true&#x27;; else echo &#x27;false&#x27;; fi)</span></span><br><span class="line"><span class="string">          echo &quot;changed=$&#123;file_changed&#125;&quot; &gt;&gt; $GITHUB_OUTPUT</span></span><br><span class="line"><span class="string"></span></span><br><span class="line">      <span class="bullet">-</span> <span class="attr">name:</span> <span class="string">Commit</span> <span class="string">and</span> <span class="string">push</span> <span class="string">changes</span></span><br><span class="line">        <span class="attr">if:</span> <span class="string">$&#123;&#123;</span> <span class="string">steps.git_status.outputs.changed</span> <span class="string">==</span> <span class="string">&#x27;true&#x27;</span> <span class="string">&#125;&#125;</span></span><br><span class="line">        <span class="attr">run:</span> <span class="string">|</span></span><br><span class="line"><span class="string">          cd config-chalice/chalice/$&#123;&#123; env.project_directory &#125;&#125;/.chalice</span></span><br><span class="line"><span class="string">          git config --local user.email &quot;contact@xxx.xxx&quot;</span></span><br><span class="line"><span class="string">          git config --local user.name &quot;contact-xxx&quot;</span></span><br><span class="line"><span class="string">          git config --local pull.rebase false</span></span><br><span class="line"><span class="string">          git add deployed/$&#123;&#123; github.event.inputs.environment &#125;&#125;.json</span></span><br><span class="line"><span class="string">          git commit -m &quot;Update deployed files at $(date)&quot;</span></span><br><span class="line"><span class="string">          git pull</span></span><br><span class="line"><span class="string">          git push</span></span><br></pre></td></tr></table></figure>
<p>在这个工作流中，我们定义了两个输入参数：<code>branch</code> 和 <code>environment</code>。这允许我们在启动工作流时指定要部署的分支和环境。我们还设置了环境变量，这些变量在部署过程中会被用来配置应用。</p>
<p>接下来，工作流会检出指定分支的代码，删除现有的 Chalice 配置文件，并从 CI&#x2F;CD 仓库中检出新的配置。然后，我们设置 Python 环境，安装依赖，准备配置文件，并配置 AWS 凭证。最后，我们执行 Chalice 部署命令，将应用部署到 AWS Lambda。</p>
<h2 id="GitHub-Action-缓存功能"><a href="#GitHub-Action-缓存功能" class="headerlink" title="GitHub Action 缓存功能"></a>GitHub Action 缓存功能</h2><p>在我们的工作流中，<code>actions/cache</code> 用于缓存 Python 的 pip 依赖。这可以显著提高后续部署的效率，因为依赖项不需要每次都重新下载。缓存的键是基于操作系统和依赖文件的哈希值，这确保了缓存的一致性和可恢复性。</p>
<h2 id="CI-CD-仓库中的-JSON-文件推送"><a href="#CI-CD-仓库中的-JSON-文件推送" class="headerlink" title="CI&#x2F;CD 仓库中的 JSON 文件推送"></a>CI&#x2F;CD 仓库中的 JSON 文件推送</h2><p>在部署完成后，Chalice 生成的 JSON 文件包含了部署的详细信息。将这个文件推送回 CI&#x2F;CD 仓库有助于我们跟踪部署历史，管理配置，并在必要时进行回滚。这是一种确保部署过程透明和可审计的重要实践。</p>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>通过这个详细的 GitHub Action 工作流，我们实现了 Chalice 应用的自动化部署。这个过程不仅简化了部署步骤，还提高了部署的可靠性。我希望这篇文章能够帮助你更好地理解如何利用 GitHub Action 来优化你的 CI&#x2F;CD 流程。</p>
<p>如果你有任何问题，或者想要了解更多关于这个话题的信息，欢迎留言讨论。别忘了点赞和分享哦！</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechat-reward.png" alt="老杨 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay-reward.png" alt="老杨 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
          <a target="_blank" class="social-link" href="https://twitter.com/6fool">
            <span class="icon">
              <i class="fab fa-twitter"></i>
            </span>

            <span class="label">Twitter</span>
          </a>
      </div>

      <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/tags/Chalice/" rel="tag"># Chalice</a>
              <a href="/tags/Python/" rel="tag"># Python</a>
              <a href="/tags/DevOps/" rel="tag"># DevOps</a>
              <a href="/tags/CI-CD/" rel="tag"># CI/CD</a>
              <a href="/tags/GitHub-Action/" rel="tag"># GitHub-Action</a>
              <a href="/tags/AWS-Lambda/" rel="tag"># AWS-Lambda</a>
              <a href="/tags/Automation/" rel="tag"># Automation</a>
              <a href="/tags/Deployment/" rel="tag"># Deployment</a>
              <a href="/tags/Serverless/" rel="tag"># Serverless</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/07/%E7%94%A8%20GitHub%20CLI%20%E6%9D%A5%E6%9F%A5%E7%9C%8B%20GitHub%20Action%20%E6%89%A7%E8%A1%8C%E6%83%85%E5%86%B5/index.html" rel="prev" title="用 GitHub CLI 来查看 GitHub Action 执行情况">
                  <i class="fa fa-angle-left"></i> 用 GitHub CLI 来查看 GitHub Action 执行情况
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/09/%E7%9B%91%E6%8E%A7%20AWS%20%E7%9A%84%20RDS%20%E5%B9%B6%E9%80%9A%E8%BF%87%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%8A%A5%E8%AD%A6/index.html" rel="next" title="监控 AWS 的 RDS 并通过企业微信来报警">
                  监控 AWS 的 RDS 并通过企业微信来报警 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">老杨</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
